package com.may.wind.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * 角色信息
 */
@Entity
@Table(name = "roles")
@JsonIgnoreProperties({"handler", "hibernateLazyInitializer"})
@ApiModel(description = "角色信息")
public class Role implements Serializable {
    @Id
    @SequenceGenerator(sequenceName = "role_sequence", name = "sequence_roles", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence_roles")
    @Column(name = "id")
    @ApiModelProperty(name = "id", value = "ID", required = true)
    long id;
    @ApiModelProperty(name = "name", value = "角色名称", required = true)
    String name;

    @Column(name = "name_zh")
    @ApiModelProperty(name = "nameZh", value = "中文名")
    private String nameZh;

    @ApiModelProperty(name = "enabled", value = "是否启用")
    private boolean enabled;

    /**
     * mapperBy为User的roles属性
     * 使用Set代替List，解决“cannot simultaneously fetch multiple bags”问题
     */
    @ManyToMany(mappedBy = "roles", cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
    Set<User> users = new HashSet<>();

    @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
    @JoinTable(name = "role_menus", joinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "menu_id", referencedColumnName = "id")})
    Collection<Menu> menus = new ArrayList<>();

    @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
    @JoinTable(name = "role_permissions", joinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "permission_id", referencedColumnName = "id")})
    Set<Permission> permissions =new HashSet<>();

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNameZh() {
        return nameZh;
    }

    public void setNameZh(String nameZh) {
        this.nameZh = nameZh;
    }

    public boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public Collection<Menu> getMenus() {
        return menus;
    }

    public void setMenus(Collection<Menu> menus) {
        this.menus = menus;
    }

    public void setPermissions(Set<Permission> permissions) {
        this.permissions = permissions;
    }

    public Set<Permission> getPermissions() {
        return permissions;
    }
}
